package com.mbengue.corp.artisan.vue.fenetre;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.mbengue.corp.artisan.controleur.Controleur;

public class ChoixArticle extends JFrame {
  private static final long serialVersionUID = -9042644360841833707L;
  private final JTable tableau;
  private final JButton valider;
  private final JButton nouvelle;
  private final JButton supprimer;
  private final Controleur controleur;
  private final JComboBox articles;
  private int nbArticles = 0;

  public ChoixArticle(Controleur controleur, Object[][] donnees) {
    super("Ajouter Articles");
    this.setSize(400, 300);
    this.setLocationRelativeTo(null);

    this.tableau = new JTable();
    this.valider = new JButton("Valider");
    this.valider.setEnabled(false);
    this.nouvelle = new JButton("Nouvelle Ligne");
    this.supprimer = new JButton("Supprimer Ligne");
    this.supprimer.setEnabled(false);
    this.controleur = controleur;
    this.articles = new JComboBox(this.controleur.tousLesCodesArticle().toArray());

    this.initTableau(donnees);
    this.ajouterEcouteursBoutons();
    JPanel pan = new JPanel();
    this.getContentPane().add(new JScrollPane(this.tableau), BorderLayout.CENTER);
    pan.add(this.nouvelle, BorderLayout.WEST);
    pan.add(this.supprimer, BorderLayout.CENTER);
    pan.add(this.valider, BorderLayout.EAST);
    pan.setVisible(true);
    this.getContentPane().add(pan, BorderLayout.SOUTH);
    this.setVisible(true);
  }

  private void initTableau(Object[][] donnees) {
    String[] titres = { "code", "designation" };
    final TableModel model;
    if (donnees == null) {
      model = new DefaultTableModel(titres, 0);
    } else {
      model = new DefaultTableModel(donnees, titres);
      this.nbArticles = donnees.length;
    }

    this.articles.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        String code = (String) articles.getSelectedItem();
        if (code != null && code.length() == 4) {
          String designation = controleur.getDesignationArticle(code);
          model.setValueAt(designation, nbArticles, 1);
          supprimer.setEnabled(true);
          valider.setEnabled(true);
          nbArticles++;
        }
      }
    });
    this.tableau.setModel(model);
    this.tableau.repaint();
  }

  private void ajouterEcouteursBoutons() {
    this.nouvelle.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        ((DefaultTableModel) tableau.getModel()).addRow(new Object[] { null, null });
        tableau.getModel().setValueAt(articles, nbArticles, 0);
        tableau.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(articles));
        tableau.repaint();
        tableau.setVisible(true);
      }
    });

    this.supprimer.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int index = tableau.getSelectedRow();
        if (index >= 0) {
          ((DefaultTableModel) tableau.getModel()).removeRow(index);
          --nbArticles;
          tableau.repaint();
          tableau.setVisible(true);
        }
      }
    });
  }

  public JTable getTableau() {
    return tableau;
  }

  public JButton getValider() {
    return valider;
  }

  public JButton getNouvelle() {
    return nouvelle;
  }

  public JButton getSupprimer() {
    return supprimer;
  }

  public int getNbArticles() {
    return nbArticles;
  }

  public Controleur getControleur() {
    return controleur;
  }
}
